package edu.ustb.efws.pipeline.collect;

import edu.ustb.efws.EfwsProperties;
import edu.ustb.efws.base.domain.Detector;
import edu.ustb.efws.pipeline.domain.Ipt;
import edu.ustb.efws.pipeline.domain.Kyj;
import edu.ustb.efws.pipeline.domain.Thos;
import edu.ustb.efws.pipeline.mapper.IptMapper;
import edu.ustb.efws.pipeline.mapper.KyjLogMapper;
import edu.ustb.efws.pipeline.mapper.KyjMapper;
import edu.ustb.efws.pipeline.mapper.ThosMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 采集数据业务服务
 *
 * @author TS Group
 */
@Service
@Transactional(readOnly = true)
public class CollectService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CollectService.class);

    private final IptMapper iptMapper;
    private final ThosMapper thosMapper;
    private final KyjMapper kyjMapper;
    private final EfwsProperties properties;

    @Autowired
    public CollectService(IptMapper iptMapper, ThosMapper thosMapper,
                          KyjMapper kyjMapper, EfwsProperties properties) {

        this.iptMapper = iptMapper;
        this.thosMapper = thosMapper;
        this.kyjMapper = kyjMapper;
        this.properties = properties;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Optional<Collect> collect(Detector detector){

        LOGGER.debug("Collect data detector={}", detector.toString());
        Collect.Type type = getType(detector);
        List<Object> data = null;
        if(type == Collect.Type.IPT){
            data = new ArrayList<>(collectIpt(detector.getTableName()));
        }
        if(type == Collect.Type.THOS){
            data = new ArrayList<>(collectThos(detector.getTableName()));
        }
        if(type == Collect.Type.KYJ){
            data = new ArrayList<>(collectKyj(detector.getTableName()));
        }
        return data == null || data.isEmpty()?
                Optional.empty():
                Optional.of(new Collect(detector.getId(), detector.getTableName(), type, data));
    }

    private Collect.Type getType(Detector detector){
        if(StringUtils.equals(detector.getDetectorType(), "智能功率变送器")){
            return Collect.Type.IPT;
        }
        if(StringUtils.equals(detector.getDetectorType(), "温湿度氧气传感器")){
            return Collect.Type.THOS;
        }
        return Collect.Type.KYJ;
    }

    /**
     * 采集ipt数据
     *
     * @param tableName 采集表名
     * @return 采集数据
     */
    private List<Ipt> collectIpt(String tableName){
        List<Ipt> data = iptMapper.find(tableName, properties.getCollectLimit());
        if(!data.isEmpty()){
            iptMapper.delete(tableName, data.stream().mapToLong(Ipt::getId).toArray());
        }
        return data;
    }

    /**
     * 采集thos数据
     *
     * @param tableName 采集表名
     * @return 采集数据
     */
    private List<Thos> collectThos(String tableName){
        List<Thos> data = thosMapper.find(tableName, properties.getCollectLimit());
        if(!data.isEmpty()){
            thosMapper.delete(tableName, data.stream().mapToLong(Thos::getId).toArray());
        }
        return data;
    }

    /**
     * 采集kyj数据
     *
     * @param tableName 采集表名
     * @return 采集数据
     */
    private List<Kyj> collectKyj(String tableName){
        List<Kyj> data = kyjMapper.find(tableName, properties.getCollectLimit());
        if(!data.isEmpty()){
            kyjMapper.delete(tableName, data.stream().mapToLong(Kyj::getId).toArray());
        }
        return data;
    }
}
